Skip to content

Conversation

@docto-rin
Copy link
Owner

# is odd num of 1 when expressing integer (k - 1) in (n - 1) bit?
is_odd = False
for shift in range(n - 1):
if k - 1 >> shift & 1:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

自分が C++ で書く場合は、演算子の優先順位を明示的に表現するために、

if (k - 1) >> (shift & 1):

と書くと思います。一方、 Google Python Style Guide には

https://google.github.io/styleguide/pyguide.html#33-parentheses

Use parentheses sparingly.
It is fine, though not required, to use parentheses around tuples. Do not use them in return statements or conditional statements unless using parentheses for implied line continuation or to indicate a tuple.

と書かれており、どちらが良いか判断できませんでした。

チームの平均的な書き方に合わせることをおすすめします。

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

上記のコード間違っていました…。元のコードを読み間違えてしまいました…。失礼しました…。

def kthGrammar(self, n: int, k: int) -> int:
bit = 0
for shift in range(k.bit_length()):
bit ^= k - 1 >> shift & 1
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好みかもしれませんが、k - 1 & 1 << shiftとした方がわかりやすく思いました。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

単に置き換えても動かないように思います。bit に 1 より大きい値が xor されるためです。
以下のように確かめた bit が立っているかどうかで場合分けが必要かと思います。
あと毎回 k - 1 している点も可読性の低さに影響しているので、最初に -1 するのもありかなと思いました。

    def kthGrammar(self, n: int, k: int) -> int:
        bit = 0
        k -= 1
        for shift in range(k.bit_length()):
            target_bit = k & 1 << shift
            if target_bit != 0:
                bit ^= 1
            # bit ^= int(target_bit != 0)
        return bit

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます。

単に置き換えても動かないように思います。bit に 1 より大きい値が xor されるためです。

習慣化していて気づいてなかったです。その通りで、1の位同士でのXORが必要です。

あと毎回 k - 1 している点も可読性の低さに影響しているので、最初に -1 するのもありかなと思いました。

こちら、言われてみて気づきました。引数の上書きは若干避けたい気持ちがあるので、自分ならtarget = k - 1とする気がします。

下記ぐらいがはっきりしていていいかもしれません。

class Solution:
    def kthGrammar(self, n: int, k: int) -> int:
        target = k - 1
        is_odd = 0  # num "1" is odd ?
        for shift in range(target.bit_length()):
            looking_bit = target >> shift 
            is_odd ^= looking_bit
        return is_odd

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直上のコード、& 1が抜けておりました。

class Solution:
    def kthGrammar(self, n: int, k: int) -> int:
        target = k - 1
        is_odd = 0  # num bit 1 is odd ?
        for shift in range(target.bit_length()):
            looking_bit = target >> shift & 1
            is_odd ^= looking_bit
        return is_odd

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

単に置き換えても動かないように思います。

あ、その通りですね。失礼しました。

whileで回す選択肢もありかもしれません。

class Solution:
    def kthGrammar(self, n: int, k: int) -> int:
        bit = 0
        target = k - 1
        while target:
            bit ^= target & 1
            target >> 1
        return bit

```python3
class Solution:
def kthGrammar(self, n: int, k: int) -> int:
return (k - 1).bit_count() % 2
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ちょっとこの関数の具体的なユースケースは思い付かないのですが、一応nを与えられたときにあり得ない(n行目に存在しない)kの値だったらエラーなどで弾いても良さそうな気がしました。


- [コメント集](https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.adit16u7jkla)
- https://discord.com/channels/1084280443945353267/1200089668901937312/1216054396161622078
- [実装2](#実装2)だが、1を数える組み込みメソッドint.bit_count()があるらしい。
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ときどき、docs.python.org を見ておくと良いでしょう。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants